<?php
function doGet(){
	if(isset($_GET['code'])){
		$code = $_GET['code'];
//		p($config_api);
		$access_key = getAccessToken(KEY,SECRET,CALLBACK_URL,$code);
		if("" != $access_key){
			$session_key =  getSessionKey($access_key);
			$_SESSION['session_key']=$session_key;
			if("" != $session_key){	
				return sendApi($session_key);
			}else{
			   echo "获取session_key失败!";  
			}
		}else{
			echo "获取access_key失败!";
		}
	}
}
/**获取access_key
 * param $client_id        应用的api_key
 * param $client_secret    应用的secret_key
 * param $redirect_uri     回调的url
 * param $code             之前获取到的Authorization Code
 * param $grant_type       "authorization_code"
 * return string
 * */
function getAccessToken($client_id,$client_secret,$redirect_uri,$code){
	$param = array(
		 'client_id' => $client_id,
		 'client_secret' => $client_secret,
		 'grant_type' => 'authorization_code',
		 'code' => $code,
	     'redirect_uri' => $redirect_uri
	);
	$mycurl = new MyCurl();
//	$result = $mycurl->get($url,$param);
	$result = $mycurl->http(ACCESS_TOKEN,"POST",$param);
	if(is_string($result)){
		$result = json_decode($result,true);
//		p($result);
		return $result['access_token'];
	}
}
/*获取session_key
 * param   $oauth_token    之前获取得access_key
 * return string
 * */
function getSessionKey($oauth_token){
	$param = array( 
		'oauth_token' => $oauth_token
	);
	$mycurl = new MyCurl();
//	$result = $mycurl->get($url,$param);
	$result = $mycurl->http(SESSION_URL,"POST",$param);
	if(is_string($result)){
		$result = json_decode($result,true);
		return $result['renren_token']['session_key'];
	}
}
/**
 *得到用户信息
 * */
function sendApi($session_key){
	$uids=246426333;
	$sig = getUserInfoSig(KEY,SECRET,$session_key,$uids);
	$param = array(
			'api_key' => KEY,
			'method' => 'users.getInfo',   //那这个api做例子
			'call_id' => time(),
			'sig' => $sig,
			'v' => '1.0',
			'uids' => $uids,        //制定获取哪个个用户的信息
			'format' => 'json',           //制定返回的数据格式
			'session_key' => $session_key  
		);
	$mycurl = new MyCurl();
	$result = $mycurl->post(API_URL,$param);
	return json_decode($result,true);
}
